//
//  PictureView.m
//  UI06_UIScrollView_UIPageControl
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "PictureView.h"
#import "SIScrollView.h"

@interface PictureView ()<UIScrollViewDelegate>
@property (nonatomic,retain) SIScrollView *scrollView;
@property (nonatomic,retain) UIPageControl *pageControl;
@end

@implementation PictureView
-(void)dealloc{
    [_scrollView release];
    [_imageArray release];
    [_pageControl release];
    [super dealloc];
}


-(instancetype)initWithFrame:(CGRect)frame{
    self =[super initWithFrame:frame];
    if (self){
        self.imageArray = [NSArray array];
        [self creatSubViews];
    }
    return self;
}

/** 创建子视图. */
-(void)creatSubViews {
    /** 创建scrollView,并添加到父视图上. */
    self.scrollView = [[SIScrollView alloc] initWithFrame:self.bounds];
    self.scrollView.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.scrollView];
    self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
    self.scrollView.delegate = self;
    
    
    [self.scrollView release];
    /** 添加图片. */
  //  [self.scrollView addPictures:self.imageArray];
    
    /** 创建pageControl,并添加到父视图上. */
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) * 0.8 , CGRectGetWidth(self.bounds), 30)];
    [self addSubview:self.pageControl];
    self.pageControl.pageIndicatorTintColor = [UIColor blackColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self.pageControl release];
    
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}
/** pageControl的点击方法 */
-(void)pageAction:(UIPageControl *)pageControl{
    /** 重点: scrollView 的偏移量 
     *  x代表的是水平方向的偏移量
     *  y代表的是垂直方向的偏移量
     *  它们都是相对于滚动范围的原点来说的.
     */
    //通过对原有的偏移量加一个pictureView的width的,使pageControl点位对齐偏移量
    self.scrollView.contentOffset = CGPointMake((pageControl.currentPage + 1) * CGRectGetWidth(self.bounds), 0);
    //当点击pageControl时重置所有图片的大小
    for(UIScrollView *scrollView in [self.scrollView subviews]){
        scrollView.zoomScale = 1.0;
    }
}

//重写setter方法
-(void)setImageArray:(NSArray<UIImage *> *)imageArray{
    if(_imageArray != imageArray){
        [_imageArray release];
        _imageArray = [imageArray retain];
    }
    /** setter方法中对scrollView添加图片数组 */
    [self.scrollView addPictures:_imageArray];
    /** 对内层小的scrollView进行属性的设置- -|| */
    for(UIScrollView *scrollView in [self.scrollView subviews]){
        scrollView.delegate = self;
        scrollView.maximumZoomScale = 3.0;
        scrollView.minimumZoomScale = 0.5;
    }
    
    self.pageControl.numberOfPages = _imageArray.count;
}
/** UIScrollView的代理方法 */
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //根据大scrollView的偏移量对齐pageControl点位
    self.pageControl.currentPage = scrollView.contentOffset.x / CGRectGetWidth(self.bounds) - 1;
    if(scrollView.contentOffset.x == 0){
        self.pageControl.currentPage = self.imageArray.count - 1;
    } else if (scrollView.contentOffset.x == (self.imageArray.count + 1) * CGRectGetWidth(self.bounds)){
        self.pageControl.currentPage = 0;
    }
    //滑动已经结束时,重置所有图片大小
    for(UIScrollView *scrollView in [self.scrollView subviews]){
        scrollView.zoomScale = 1.0;
    }
    //当图片滑到头尾的图时改变偏移量,跳转.
    if(scrollView.contentOffset.x == 0){
        scrollView.contentOffset = CGPointMake((self.imageArray.count) * CGRectGetWidth(self.bounds), 0);
    } else if (scrollView.contentOffset.x == (self.imageArray.count + 1) *CGRectGetWidth(self.bounds)){
        scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
    }
}


-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return scrollView.subviews.firstObject;
}


@end
